package system

import (
	"context"
	v1 "gf-vben-admin/api/system/v1"
	model "gf-vben-admin/internal/model/system"
	service "gf-vben-admin/internal/service/system"
	"github.com/gogf/gf/v2/util/gconv"
)

var (
	Permission = cPermission{}
)

type cPermission struct{}

//PageList 分页查询API表
func (c *cPermission) PageList(ctx context.Context, req *v1.PermissionPageListReq) (res *v1.PermissionPageListRes, err error) {
	total, list, err := service.Permission().PageList(ctx, req.Permission)
	if err != nil {
		return nil, err
	}
	res = &v1.PermissionPageListRes{
		Total: total,
		Rows:  list,
	}
	return
}

//List 列表查询API表
func (c *cPermission) List(ctx context.Context, req *v1.PermissionListReq) (res *v1.PermissionListRes, err error) {
	list, err := service.Permission().List(ctx, req.Permission)
	if err != nil {
		return nil, err
	}
	res = &v1.PermissionListRes{
		Rows: list,
	}
	return
}

// Get 查询API表详情
func (c *cPermission) Get(ctx context.Context, req *v1.PermissionGetReq) (res *v1.PermissionGetRes, err error) {
	entity, err := service.Permission().Get(ctx, req.Id)
	if err != nil {
		return nil, err
	}
	res = &v1.PermissionGetRes{
		Permission: entity,
	}
	return
}

//Add 添加API表
func (c *cPermission) Add(ctx context.Context, req *v1.PermissionAddReq) (res *v1.PermissionAddRes, err error) {
	in := new(model.Permission)
	err = gconv.Struct(req, in)
	if err != nil {
		return nil, err
	}
	err = service.Permission().Add(ctx, *in)
	return
}

//Edit 编辑API表
func (c *cPermission) Edit(ctx context.Context, req *v1.PermissionEditReq) (res *v1.PermissionEditRes, err error) {
	in := new(model.Permission)
	err = gconv.Struct(req, in)
	if err != nil {
		return nil, err
	}
	err = service.Permission().Edit(ctx, *in)
	return
}

//EditState 编辑API表状态
func (c *cPermission) EditState(ctx context.Context, req *v1.PermissionEditStateReq) (res *v1.PermissionEditRes, err error) {
	err = service.Permission().EditState(ctx, req.Ids, req.State)
	return
}

//Delete 删除API表
func (c *cPermission) Delete(ctx context.Context, req *v1.PermissionDelReq) (res *v1.PermissionDelRes, err error) {
	err = service.Permission().Delete(ctx, req.Ids)
	return
}
